package com.dream.testtubebaby.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.dream.testtubebaby.R;
import com.dream.testtubebaby.adapter.CustomPagerAdapter;
import com.dream.testtubebaby.utils.SharePreferencesManager;
import com.dream.testtubebaby.utils.StatusBarCompat;

public class WelcomActivity extends AppCompatActivity {
	public int[] guides = new int[] { R.drawable.guide_1, R.drawable.guide_2 };
	private ViewPager guidePager;
	private SharePreferencesManager mSharePreferencesManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mSharePreferencesManager = new SharePreferencesManager(this);
		if (!mSharePreferencesManager.readGuideFlag()) {
			if (mSharePreferencesManager.readUserId() == 0L) {
				Intent intent = new Intent(this, LoginActivity.class);
				startActivity(intent);
			} else {
				Intent intent = new Intent(this, MainActivity.class);
				startActivity(intent);
			}
			finish();
		} else {
			setContentView(R.layout.activity_welcom);
			StatusBarCompat.compat(this, getResources().getColor(R.color.status_bar_color));
			guidePager = (ViewPager) findViewById(R.id.viewGuide);
			setupGuidePage();
		}
	}

	private void setupGuidePage() {
		List<View> mList = new ArrayList<View>();
		LayoutInflater inflat = LayoutInflater.from(this);
		View item = null;

		for (int index : guides) {
			item = inflat.inflate(R.layout.page_guide, null);
			item.setBackgroundResource(index);
			mList.add(item);
		}

		item = inflat.inflate(R.layout.page_guide, null);
		item.setBackgroundResource(R.drawable.guide_3);
		TextView start = (TextView) item.findViewById(R.id.start);
		start.setVisibility(View.VISIBLE);
		start.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				guidePager.setVisibility(View.GONE);
				mSharePreferencesManager.writeGuideFlag(false);
				if (mSharePreferencesManager.readUserId() == 0) {
					Intent intent = new Intent();
					intent.setClass(WelcomActivity.this, LoginActivity.class);
					startActivity(intent);
				} else{
					Intent intent = new Intent();
					intent.setClass(WelcomActivity.this, MainActivity.class);
					startActivity(intent);
				}
				finish();
			}
		});
		mList.add(item);

		guidePager.setAdapter(new CustomPagerAdapter(mList));
	}
}
